<?php
global $_W,$_GPC;

$settings = get("select ranks from ".tb('scorerank')." where uniacid='{$_W['uniacid']}'");
$set = unserialize($settings['ranks']);
if (checksubmit('submit')){
	$data = $_GPC['data'];
	
	$home_adv = array();
	$home_picurl = $_GPC['home_picurl'];
	if (!empty($home_picurl)){
		foreach ($home_picurl as $key => $value) {
			if (!empty($value)){
				$home_adv[] = array(
					'pic'=>$value,
					'link'=>trim($_GPC['home_link'][$key]),
					'sort'=>$_GPC['home_sort'][$key],
				);
			}
		}
		array_multisort($_GPC['home_sort'],SORT_DESC,$home_adv);
	}
	$data['adv'] = $home_adv;
	
	if ($data['kword'] != $set['kword']){
		$rule = get("select id from ".tablename('rule')." where uniacid='{$_W['uniacid']}' and module='junsion_wxtools' and name='junsion_wxtools_rank'");
		if (empty($rule)){
			$rule = array(
				'uniacid' => $_W['uniacid'],
				'name' => 'junsion_wxtools_rank',
				'module' => $this->modulename,
				'status' => 1,
				'displayorder' => 254,
			);
			pdo_insert('rule',$rule);
			$rule['id'] = pdo_insertid();
		}
		$rk = pdo_fetch('select id from '.tablename('rule_keyword')." where rid='{$rule['id']}' and content='{$set['kword']}' limit 1");
		if (empty($rk)){
			$rulew = array(
					'uniacid' => $_W['uniacid'],
					'module' => $this->modulename,
					'status' => 1,
					'displayorder' => 254,
					'type'=>1,
					'rid'=>$rule['id'],
					'content'=>$data['kword'],
			);
			pdo_insert('rule_keyword',$rulew);
		}else pdo_update('rule_keyword',array('content'=>$data['kword']),array('id'=>$rk['id']));
	}
	
	if ($settings){
		update('scorerank',array('ranks'=>serialize($data)),array('uniacid'=>$_W['uniacid']));
	}else insert('scorerank',array('ranks'=>serialize($data),'uniacid'=>$_W['uniacid']));
	
	MSG('修改成功','referer');
}


include $this->template('scorerank');